/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets.model;

import java.io.Serializable;

/**
 * Models a taxonomy term.
 *
 * @version 1.0
 * @author August Detlefsen for CodeMagi, Inc.
 */
public class Term extends BaseBean implements Serializable, Comparable {

    private Category category;
    private String shortName;
    private String description;
    private String alias;
    private Integer sortOrder;
    private String toolTip;
    private Boolean display = true;  //whether the Term should be displayed on lists, taglibs, etc. 

    //GETTERS AND SETTERS -------------------------------------------------
    
    public void setCategory(Category newValue) {
	category = newValue;
    }

    public Category getCategory() {
	return category;
    }

    public void setShortName(String newValue) {
	shortName = newValue;
    }

    public String getShortName() {
	return shortName;
    }

    public void setDescription(String newValue) {
	description = newValue;
    }

    public String getDescription() {
	return description;
    }

    public void setAlias(String newValue) {
	alias = newValue;
    }

    public String getAlias() {
	return alias;
    }

    public void setSortOrder(Integer newValue) {
	sortOrder = newValue;
    }

    public Integer getSortOrder() {
	return sortOrder;
    }

    public void setToolTip(String newValue) {
	toolTip = newValue;
    }

    public String getToolTip() {
	return toolTip;
    }

    public void setDisplay(Boolean newValue) {
	display = newValue;
    }

    public Boolean getDisplay() {
	return display;
    }

    
    //SPECIAL GETTERS AND SETTERS ------------------------------------

    public Integer getCategoryId() {
	if (category == null) {
	    return null;
	}

	return category.getId();
    }

    public String getCategoryName() {
	if (category == null) {
	    return "";
	}

	return category.getName();
    }

    //IMPLEMENT Comparable interface
    public int compareTo(Object o) {

	int output = 0;

	try {
	    Term t = (Term) o;

	    if (sortOrder != null && t.getSortOrder() != null) {
		output = sortOrder.compareTo(t.getSortOrder());

	    } else if (sortOrder == null && t.getSortOrder() != null) {
		output = 1;

	    } else if (sortOrder != null && t.getSortOrder() == null) {
		output = -1;

	    } 
	    
	    //if there is no difference in sort order, use name
 	    if (output == 0) {
		output = getName().toUpperCase().compareTo(t.getName().toUpperCase());

	    }

	} catch (Exception e) {
	    //NullPointerException, ClassCastException, for example                                                         
	}

	return output;
    }

    @Override
    public String toString() {
	return super.toString() + " [ name: " + getName() + " sortOrder: " + sortOrder + " ] ";
    }
}
